package com.wn.sk.order.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.common.api.order.dto.OrderDto;
import com.wn.common.api.order.vo.OrderVo;
import com.wn.sk.order.core.contants.OrderConstant;
import com.wn.sk.order.core.entity.Order;
import com.wn.sk.order.core.entity.OrderItem;
import com.wn.sk.order.core.mapper.OrderMapper;
import com.wn.sk.order.core.service.IOrderItemService;
import com.wn.sk.order.core.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author yellow
 * @since 2022-09-08
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 创建订单操作
     * @param dto
     * @return
     */
    @Override
    public String createOrder(OrderDto dto) {

        // 01  创建一个订单对象order
        // 给订单数值部分字段
        /* 往 pms_order 表保存数据 */
        Order order = new Order();
        //订单编号
        order.setOrderSn(UUID.randomUUID().toString());
        order.setCreateTime(new Date());
        order.setUserId(dto.getUserId());

        //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        order.setStatus(OrderConstant.ORDER_STATUS_UNPAY);

        //订单来源：0->PC订单；1->app订单
        order.setSourceType(OrderConstant.ORDER_SOURCE_PC);
        //订单类型：0->正常订单；1->秒杀订单
        order.setOrderType(OrderConstant.ORDER_SECKILL_TYPE);
        order.setTotalAmount(dto.getPromotionPrice());
        this.save(order);

        // 02  创建一个订单详情对象orderItem
        OrderItem orderItem = new OrderItem();
        //订单id
        orderItem.setOrderId(order.getId());
        //订单编号
        orderItem.setOrderSn(order.getOrderSn());
        orderItem.setProductId(dto.getProductId());
        orderItem.setProductPic(dto.getProductPic());
        orderItem.setProductName(dto.getProductName());
        orderItem.setProductPrice(dto.getPrice());
        orderItem.setPromotionAmount(dto.getPromotionPrice());
        orderItem.setProductQuantity(1);
        orderItemService.save(orderItem);

        //返回订单编号
        return order.getOrderSn();
    }

    @Override
    public OrderVo findByOrderSn(String orderSn) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_sn",orderSn);
        Order order = this.getOne(queryWrapper);

        OrderVo ordervo = new OrderVo();
        //order  需要 转成 ordervo ？
        // 这个是
        BeanUtils.copyProperties(order,ordervo);

        return ordervo;
    }

    @Override
    public Integer updateByOrderSn(String orderSn) {
        LambdaQueryWrapper<Order> qr = Wrappers.lambdaQuery(Order.class);
        qr.eq(Order::getOrderSn,orderSn);
        Long queryCount = baseMapper.selectCount(qr);
        log.info("根据订单编号查询订单 {}",queryCount);
        if(queryCount == 0){
            throw new RuntimeException("订单不存在");
        }
        // 修改状态为待发货
        LambdaUpdateWrapper<Order> updateWrapper = Wrappers.lambdaUpdate(Order.class);
        updateWrapper.set(Order::getStatus,OrderConstant.ORDER_STATUS_UNSHIP);
        updateWrapper.eq(Order::getOrderSn,orderSn);
        this.update(updateWrapper);

        return OrderConstant.ORDER_STATUS_UNSHIP;
    }
}
